<?php
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.view');

class igcategoriesViewEditorlist extends JView
{
	function display($tpl = null)
	{
		global $mainframe;

	    $db   =& JFactory::getDBO();
		$doc  =& JFactory::getDocument();

		$name = JRequest::getWord('e_name');
		$rand = rand(1,9999);

	    /** MAKE CATEGORY LIST **/

		$query = "SELECT * FROM #__igallery WHERE published = 1 ORDER BY parent, ordering";
	    $db->setQuery( $query );

	    $categories = $db->loadObjectList();

	    //make the tree
	    $children = array();
		if($categories )
	    {
	        foreach($categories as $v)
	        {
	            $pt     = $v->parent;
	            $list   = @$children[$pt] ? $children[$pt] : array();
	            array_push($list,$v);
	            $children[$pt] = $list;
	        }
	    }
		$categoryTree = JHTML::_('menu.treerecurse', 0, '', array(), $children, 9999, 0, 0 );

		//make the top select item
	    $selectItems = array();

	    //make the rest of the select items
	    foreach ($categoryTree as $branch)
	    {
	        $selectItems[] = JHTML::_('select.option',$branch->id, '&nbsp;&nbsp;&nbsp;'. $branch->treename);
	    }

	    $categoryList = JHTML::_('select.genericlist', $selectItems, 'igalleryCategorySelect','class="inputbox" ','value','text','','igalleryCategorySelect' );

	    /** MAKE PROFILE LIST **/

	    $db->setQuery( "SELECT id as value, name as text FROM #__igallery_profiles WHERE published = 1 ORDER BY ordering" );
	    $profiles = $db->loadObjectList();
	    $profileList = JHTML::_("select.genericlist", $profiles, 'igalleryProfileSelect', 'class="inputbox"', 'value', 'text', '', 'igalleryProfileSelect' );

        /** MAKE TYPE LIST **/

        $typeOptions = array();
        $typeOptions[] = JHTML::_('select.option','classic', JText::_('CATEGORY'));
        $typeOptions[] = JHTML::_('select.option','random', JText::_('RANDOM IMAGES'));
        $typeOptions[] = JHTML::_('select.option','latest', JText::_('LATEST IMAGES'));
        $typeOptions[] = JHTML::_('select.option','rated', JText::_('HIGHEST RATED'));
        $typeOptions[] = JHTML::_('select.option','commented', JText::_('MOST COMMENTED'));
        $typeOptions[] = JHTML::_('select.option','tagged', JText::_('TAGGED'));
        $typeOptions[] = JHTML::_('select.option','hits', JText::_('MOST VIEWED'));
        $typeList = JHTML::_("select.genericlist", $typeOptions, 'igalleryTypeSelect', 'class="inputbox"', 'value', 'text', '', 'igalleryTypeSelect' );

        /** MAKE CHILDREN LIST **/

        $childrenOptions = array();
        $childrenOptions[] = JHTML::_('select.option','1', JText::_('YES'));
        $childrenOptions[] = JHTML::_('select.option','0', JText::_('NO'));
        $childrenList = JHTML::_("select.genericlist", $childrenOptions, 'igalleryChildren', 'class="inputbox"', 'value', 'text', '', 'igalleryChildren' );
        
        /** MAKE SHOW MENU LIST **/
		$showMenuOptions = array();
        $showMenuOptions[] = JHTML::_('select.option','1', JText::_('YES'));
        $showMenuOptions[] = JHTML::_('select.option','0', JText::_('NO'));
        $showMenuList = JHTML::_("select.genericlist", $showMenuOptions, 'igalleryShowMenu', 'class="inputbox"', 'value', 'text', '', 'igalleryShowMenu' );

	    $js = '
	    function insertToken() {
	        //o = _getUriObject(window.self.location.href);
	        //q = _getQueryObject(o.query);
	        //var editor = decodeURIComponent(q.e_name);
    		var catid = $(\'igalleryCategorySelect\').value;
    		var proid = $(\'igalleryProfileSelect\').value;
    		var typeid = $(\'igalleryTypeSelect\').value;
    		var children = $(\'igalleryChildren\').value;
    		var showmenu = $(\'igalleryShowMenu\').value;
    		var tagid = $(\'igalleryTagList\').value;
    		var limit = $(\'igalleryLimit\').value;
    		var uid = $(\'igalleryUniqueId\').value;
    		var token = \'{igallery id="\' + uid + \'" cid="\' + catid + \'" pid="\' + proid + \'" type="\' + typeid + \'" children="\' + children + \'" showmenu="\' + showmenu + \'" tags="\' + tagid + \'" limit="\' + limit + \'"}\';
    		window.parent.jInsertEditorText(token, \''.$name.'\');
    		return false;
		}';

	    JHTML::_('behavior.mootools');

	    $doc->addScriptDeclaration($js);

		$this->assignRef('categoryList', $categoryList);
		$this->assignRef('profileList', $profileList);
		$this->assignRef('typeList', $typeList);
		$this->assignRef('childrenList', $childrenList);
		$this->assignRef('showMenuList', $showMenuList);
		$this->assignRef('rand', $rand);

		parent::display($tpl);
    }
}